package com.zhj.leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * 力扣22 括号生成
 * @author zhj
 */
public class Test22 {
    public static void main(String[] args) {
        List<String> fun = fun(3);
        for (String s : fun) {
            System.out.println(s);
        }
    }

    public static List<String> fun(int n) {
        List<String> result = new ArrayList<>();
        backtracking(n, result , 0, 0,"");
        return result;
    }

    private static void backtracking(int n, List<String> result, int left, int right, String str) {
        if (right > left) {
            return;
        }
        if (left == right && right == n) {
            result.add(str);
            return;
        }
        if (left < n) {
            backtracking(n, result, left + 1, right, str + "(");
        }
        if (right < left) {
            backtracking(n, result, left, right + 1, str + ")");
        }
    }
}
